package com.ledim.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ledim.adapter.base.LedimBaseListAdapter;
import com.ledim.bean.LedimChoiceCardBean;
import com.ledim.itf.LedimOnItemClickListener;
import com.ledim.utils.FrescoUtil;
import com.ledim.utils.LedimBaseUtils;
import com.letv.android.client.LetvSDK;
import com.letv.android.young.client.R;
import com.letv.core.bean.IVideo;
import java.util.ArrayList;
import java.util.List;

public class GoneCardsAdapter extends LedimBaseListAdapter<LedimChoiceCardBean> {

    public boolean mSelectedType;
    public ArrayList<LedimChoiceCardBean> mSelectedCards;
    public LedimOnItemClickListener<LedimChoiceCardBean> mListener;

    public GoneCardsAdapter(Context context, LedimOnItemClickListener<LedimChoiceCardBean> listener,
                            List<LedimChoiceCardBean> list, boolean mSelectedType) {
        super(context);
        this.mSelectedType = mSelectedType;
        mSelectedCards = new ArrayList<LedimChoiceCardBean>();
        mListener = listener;
        setDatas(list);

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_gone_all_cards, null);
            holder.mImage =
                    (SimpleDraweeView) convertView.findViewById(R.id.item_gone_all_cards_img);
            holder.mEpisode = (TextView) convertView.findViewById(R.id.item_gone_all_cards_episode);
            holder.mTitle = (TextView) convertView.findViewById(R.id.item_gone_all_cards_title);
            holder.mCount = (TextView) convertView.findViewById(R.id.item_gone_all_cards_count);
            holder.mSelected = (ImageView) convertView.findViewById(R.id.selected_img);
            holder.mSelectLayout =
                    convertView.findViewById(R.id.items_gone_all_rooms_select_layout);
            holder.mLine = convertView.findViewById(R.id.item_gone_all_cards_line);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final LedimChoiceCardBean card = getItem(position);
        if (position == getCount() - 1) {
            holder.mLine.setVisibility(View.GONE);
        } else {
            holder.mLine.setVisibility(View.VISIBLE);
        }
        if (card.photo != null && !TextUtils.isEmpty(card.photo.thumb)) {
            FrescoUtil.loadUrl(card.photo.thumb, holder.mImage);
        }
        if (card.nameCn != null) {
            holder.mTitle.setText(card.nameCn);
        } else {
            if (card.title != null) {
                holder.mTitle.setText(card.title);
            }
        }
        if (card.playedDuration / 1000 != 0) {
            holder.mCount.setText(
                    "已观看" + LedimBaseUtils.secToTime1(card.playedDuration / 1000 + ""));
        } else {
            holder.mCount.setText("已观看不足1分钟");
        }

        if (card.playedEpisode != null && card.playedEpisode.length() > 0) {
            holder.mEpisode.setVisibility(View.VISIBLE);
            holder.mEpisode.setText(card.playedEpisode);
        } else {
            holder.mEpisode.setVisibility(View.GONE);
        }

        if (mSelectedType) {
            holder.mSelectLayout.setVisibility(View.VISIBLE);

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickCheckEpisodes(holder.mSelected, card);
                    if (mListener != null) {
                        mListener.onItemClick(position, card, v);
                    }
                }
            });
            if (mSelectedCards.contains(card)) {
                holder.mSelected.setImageResource(R.drawable.check);
            } else {
                holder.mSelected.setImageResource(R.drawable.unchecked);
            }

        } else {
            holder.mSelectLayout.setVisibility(View.GONE);

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IVideo roomVideo = new IVideo();
                    roomVideo.setmVid(0);
                    LetvSDK.getInstance().play(context, roomVideo, false, card);
                }
            });
        }

        return convertView;
    }

    private void onClickCheckEpisodes(ImageView mCheckMArk, LedimChoiceCardBean cardBean) {

        if (mSelectedCards.contains(cardBean)) {
            mCheckMArk.setImageResource(R.drawable.unchecked);
            mSelectedCards.remove(cardBean);
        } else {
            mSelectedCards.add(cardBean);
            mCheckMArk.setImageResource(R.drawable.check);
        }
    }

    public ArrayList<LedimChoiceCardBean> getSelectedCards() {
        return mSelectedCards;
    }

    public boolean isSelectedAll() {
        return mSelectedCards.size() == getCount();
    }

    public int getSelectedCount() {
        return mSelectedCards.size();
    }

    public void clearCards() {
        getDatas().removeAll(mSelectedCards);
        mSelectedCards.clear();
        notifyDataSetChanged();

    }

    public void selectAll() {
        mSelectedCards.clear();
        mSelectedCards.addAll(getDatas());
        notifyDataSetChanged();
    }

    class ViewHolder {

        private SimpleDraweeView mImage;
        private TextView mEpisode;
        private TextView mTitle;
        private TextView mCount;
        private View mSelectLayout;
        private View mLine;
        private ImageView mSelected;
    }

}
